package com.fitnessrocks.sweattimer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;	
import android.widget.TextView;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.fitnessrocks.sweattimer.service.TimerService;

public class MainActivity extends Activity {
	private TimerService mBoundService;
	private boolean mIsBound;
	private boolean mIsRunning;
	
	private TextView mTextUp;
	private TextView mTextDown;
	
	private EditText mEditON;
	
    private final ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  Because we have bound to a explicit
            // service that we know is running in our own process, we can
            // cast its IBinder to a concrete class and directly access it.
            mBoundService = ((TimerService.LocalBinder)service).getService();

            // Tell the user about this for our demo.
            Toast.makeText(MainActivity.this, R.string.timer_service_connected,
                    Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            // Because it is running in our same process, we should never
            // see this happen.
            mBoundService = null;
            Toast.makeText(MainActivity.this, R.string.timer_service_disconnected,
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        doBindService();
        
        mTextUp = (TextView) this.findViewById(R.id.textViewUp);
        mTextDown = (TextView) this.findViewById(R.id.textViewDown);
        mEditON = (EditText)this.findViewById(R.id.editTextON);
        
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    void doBindService() {
    	if (mIsBound) {
    		return;
    	}

        // Establish a connection with the service.  We use an explicit
        // class name because we want a specific service implementation that
        // we know will be running in our own process (and thus won't be
        // supporting component replacement by other applications).
        bindService(new Intent(this,
                TimerService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
        Thread.dumpStack();
    }

    void doUnbindService() {
        Thread.dumpStack();
        if (mIsBound) {
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }

    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (!mIsRunning) {
	        doUnbindService();
		}
	}

    @Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		doBindService();
	}

	@Override
    protected void onDestroy() {
        super.onDestroy();
        doUnbindService();
    }

	public void handleTextDown(String msg)
	{
		mTextDown.setText(msg);
	}
	
	public void handleTextUp(String msg)
	{
		mTextUp.setText(msg);
	}
	
    public void onStartClicked(View v) {
    	Toast.makeText(this, "onStartClicked", Toast.LENGTH_SHORT).show();
    	int sec = Integer.parseInt(mEditON.getText().toString());
    	int interval = 1000; // millisecond
    	
    	SweatCountDownTimer scdt = new SweatCountDownTimer(this,sec*interval,interval);
        scdt.start();
        
    	mIsRunning = true;
    }
    public void onStopClicked(View v) {
    	Toast.makeText(this, "onStopClicked", Toast.LENGTH_SHORT).show();
    	mIsRunning = false;
    }
}
